package com.zhangkunhao.notes.web.servlet;

import com.alibaba.fastjson.JSON;
import com.zhangkunhao.notes.entity.Do.PursueList;
import com.zhangkunhao.notes.entity.Do.Question;
import com.zhangkunhao.notes.entity.Do.User;
import com.zhangkunhao.notes.entity.bo.Message;
import com.zhangkunhao.notes.entity.bo.PageBean;
import com.zhangkunhao.notes.service.PursueListServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

/**
 * 用户关注列表的controller
 *
 * @author 20642
 */
@WebServlet("/pursueList/*")
public class PursueListServlet extends BaseDaoServlet {
    private PursueListServiceImpl pursueListService = new PursueListServiceImpl();

    public void listPursuingUsers(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 通过Session接收用户Id
        Integer id = (Integer) request.getSession().getAttribute("userId");

        //调用service层,封装返回前端的对象
        PageBean<User> pursueListPageBean = pursueListService.listPursuingUsers(id);

        // 转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(pursueListPageBean));
    }

    public void listMyPursueUsers(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 通过Session接收用户Id
        Integer id = (Integer) request.getSession().getAttribute("userId");

        //调用service层,封装返回前端的对象
        PageBean<User> userPageBean = pursueListService.listMyPursueUsers(id);

        // 转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(userPageBean));
    }

    public void showMyPursueUserQuestions(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //接收数据
        Integer userId = (Integer) request.getSession().getAttribute("userId");

        //调用service层
        PageBean<Question> questionPageBean = pursueListService.showMyPursueUserQuestions(userId);

        // 转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(questionPageBean));
    }

    public void rankByLikesCount(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //接收数据
        Integer userId = (Integer) request.getSession().getAttribute("userId");

        //调用service层
        PageBean<Question> questionPageBean = pursueListService.rankByLikesCount(userId);

        // 转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(questionPageBean));
    }

    public void rankByLaunchTime(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //接收数据
        Integer userId = (Integer) request.getSession().getAttribute("userId");

        //调用service
        PageBean<Question> questionPageBean = pursueListService.rankByLaunchTime(userId);

        // 转换为json数据，写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(questionPageBean));
    }


    /**
     * 关注用户
     *
     * @param request：
     * @param response：
     * @throws Exception：
     */
    public void pursueUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.get方式接收数据,乱码解决
        int pursueUserId = Integer.parseInt(new String(request.getParameter("pursueUserId")
                .getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));

        System.out.println("pursueUserId:" + pursueUserId);

        // 通过Session接收用户Id
        Integer userId = (Integer) request.getSession().getAttribute("userId");

        //2.调用service层,封装返回前端的对象
        Message<PursueList> pursueListMessage = pursueListService.pursueUser(userId, pursueUserId);

        //3.写数据，转为json
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(pursueListMessage));
    }

    public void listCommonPursueUsers(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.get方式接收数据,乱码解决
        int oppositeUserId = Integer.parseInt(new String(request.getParameter("eachPursueUserId")
                .getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
        System.out.println("eachPursueUserId:" + oppositeUserId);

        // 通过Session接收用户Id
        Integer userId = (Integer) request.getSession().getAttribute("userId");

        //2.调用service层,封装返回前端的对象
        PageBean<User> userPageBean = pursueListService.listCommonPursueUsers(userId, oppositeUserId);

        //3.写数据，转为json
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(userPageBean));
    }

    public void cancelPursueUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.get方式接收数据,乱码解决
        int cancelPursueUserId = Integer.parseInt(new String(request.getParameter("cancelPursueUserId")
                .getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
        System.out.println("cancelPursueUserId:" + cancelPursueUserId);

        // 通过Session接收用户Id
        Integer userId = (Integer) request.getSession().getAttribute("userId");

        //2.调用service层,封装返回前端的对象
        Message<PursueList> cancelPursueUser = pursueListService.cancelPursueUser(userId, cancelPursueUserId);

        //3.写数据，转为json
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(cancelPursueUser));
    }
}
